package com.folderit.apps.rooms;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.folderit.apps.rooms.data.RoomsContract;


public class ReservationsActivity extends ActionBarActivity implements ReservationsFragment.ReservationSelectedCallback {

    public static final String SELECTED_ROOM_KEY = "selected_room";
    private int mSelectedRoomId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_reservations);

        if (savedInstanceState == null) {
            // Create the reservation fragment and add it to the activity
            // using a fragment transaction.
            Intent intent = getIntent();

            ReservationsFragment fragment = new ReservationsFragment();

            if (intent.hasExtra(SELECTED_ROOM_KEY)) {
                mSelectedRoomId = getIntent().getIntExtra(SELECTED_ROOM_KEY, 0);

                Bundle arguments = new Bundle();
                arguments.putInt(SELECTED_ROOM_KEY, mSelectedRoomId);

                fragment.setArguments(arguments);
            }

            getSupportFragmentManager().beginTransaction()
                    .add(R.id.reservations_container_fragment, fragment)
                    .commit();
        }
        else {
            mSelectedRoomId = savedInstanceState.getInt(SELECTED_ROOM_KEY);
        }

        // Set the action bar icon corresponding to the selected room color
        Cursor cursor = getContentResolver().query(
                RoomsContract.RoomEntry.buildRoomUri(mSelectedRoomId),
                new String[]{RoomsContract.RoomEntry.COLUMN_COLOR_ID},
                null,
                null,
                null
        );

        if (cursor.moveToFirst())
        {
            int colorId = cursor.getInt(cursor.getColumnIndex(RoomsContract.RoomEntry.COLUMN_COLOR_ID));
            ActionBar actionBar = getSupportActionBar();
            actionBar.setIcon(Utility.getRoomIconIdByColorId(colorId));
            cursor.close();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_reservations, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // I need to store the selected room id
        // because I need it to set the icon of the activity bar
        if (mSelectedRoomId != 0) {
            outState.putInt(SELECTED_ROOM_KEY, mSelectedRoomId);
        }

        super.onSaveInstanceState(outState);
    }

    @Override
    public void onReservationSelected(int reservationId) {

        Intent intent = new Intent(this, ReservationDetailsActivity.class)
                .putExtra(ReservationDetailsActivity.SELECTED_RESERVATION_KEY, reservationId);
        startActivity(intent);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_reservations, container, false);
            return rootView;
        }
    }
}
